package com.dili.dd.gpi.controller;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dili.dd.gpi.domain.Application;
import com.dili.dd.gpi.domain.GpiSnapshot;
import com.dili.dd.gpi.domain.Site;
import com.dili.dd.gpi.service.DmaAppService;
import com.dili.dd.gpi.service.GpiSnapshotService;
import com.dili.dd.gpi.service.SiteService;
import com.dili.dd.gpi.util.spring.JsonView;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhaoYuan on 2015/10/20.
 */
@Controller
@RequestMapping(value = "/site")
public class SiteController {

    @Resource
    private SiteService siteService;

    @Resource
    private GpiSnapshotService gpiSnapshotService;

    @Resource
    private DmaAppService dmaAppService;

    @RequestMapping(value = "/save.htm",method = RequestMethod.PUT)
    public ModelAndView add(Site site){
        try {
            siteService.createSite(site);
            return new ModelAndView(new JsonView(true,"添加成功"));
        } catch (Exception e) {
            return new ModelAndView(new JsonView(false,e.getMessage()));
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ModelAndView update(Site site){
        try {
            siteService.updateSite(site);
            return new ModelAndView(new JsonView(true,"success"));
        } catch (Exception e) {
            return new ModelAndView(new JsonView(false,e.getMessage()));
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public ModelAndView delete(int id){
        try {
            siteService.deleteSite(id);
            return new ModelAndView(new JsonView(true,"success"));
        } catch (Exception e) {
            return new ModelAndView(new JsonView(false,e.getMessage()));
        }
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView list(){
        List<Site> sites = siteService.findAll();
        return new ModelAndView(new JsonView(sites, SerializerFeature.WriteMapNullValue));
    }

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String toList(){
        return "/WEB-INF/views/site/list";
    }

    @RequestMapping(value = "/{id}/",method = RequestMethod.GET)
    public String toView(@PathVariable int id,Model model){
        Site site = siteService.get(id);
        List<GpiSnapshot> allSystem = gpiSnapshotService.findAllSystem();
        List<Application> allAppList = dmaAppService.getAllAppList();

        model.addAttribute("data",site);
        model.addAttribute("webs",allSystem);
        model.addAttribute("apps",allAppList);
        return "/WEB-INF/views/site/view";
    }
}
